﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using Entity;
using Business; 


namespace WebApp
{
    public partial class ViewCustomerforAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblm.Visible = false;
            if (!IsPostBack)
                Bindcus();
        }
        private void Bindcus()
        {

            GridView1.DataSource = GridDataSource();
            GridView1.DataBind();
        }
        private DataTable GridDataSource()
        {
            Customer_Business p = new Customer_Business();
            DataTable dt = new DataTable();
            try
            {
                dt = p.Load();
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
            finally
            {
                p = null;
            }

            return dt;
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminPage.aspx");
        }

        protected void Editting(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bindcus();
        }

        protected void Cancel(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bindcus();
        }

        protected void Update(object sender, GridViewUpdateEventArgs e)
        {
            Customer_Business _ObjBusiness = new Customer_Business();
            GridViewRow row = GridView1.Rows[e.RowIndex];
            int cusID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
            int getRowCount = 0;
            //TextBox txtname = (TextBox)row.FindControl("txtname");
            //TextBox txttopic = (TextBox)row.FindControl("txttopic");
            string username = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            string password = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
            int phone = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text);
            int totalorders = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text);
            int totalprice = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text);
            getRowCount = _ObjBusiness.Update(cusID, username, password, name, address, phone, totalorders, totalprice);
            GridView1.EditIndex = -1;
            Bindcus();
        }

        protected void Delete(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
            Customer_Business pBAL = new Customer_Business();
            try
            {
                pBAL.Delete(id);
               
                lblm.Visible = true;
                lblm.Text = "Record Deleted Successfully.";
            }
            catch (Exception ee)
            {
                lblm.Text = ee.Message.ToString();
            }
            finally
            {
                pBAL = null;
            }

            GridView1.EditIndex = -1;
         
            Bindcus();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowState != DataControlRowState.Edit) // check for RowState
            {
                if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType
                {
                    string id = e.Row.Cells[0].Text; // Get the id to be deleted
                    LinkButton lb = (LinkButton)e.Row.Cells[9].Controls[0]; //cast the ShowDeleteButton link to linkbutton
                    if (lb != null)
                    {
                        lb.Attributes.Add("onclick", "return DeleteConfirmation('" + id + "');"); //attach the JavaScript function with the ID as the paramter
                    }
                }
            }
        }
    }
}